

//引入模块
var http = require('http');
var querystring = require('querystring');
var sessions = require('../tcp_server/tcp_server');
var s_db = require('../dbwrap_interface.js');
//创建服务器
var http_server = http.createServer(function (req, res) {
    console.log(req.body)
    if (req.url === '/sendCmd' && req.method.toLowerCase() === 'post') {
        var alldata = '';
        req.on('data', function (chunk) {
            alldata += chunk;
        });

        req.on('end', function () {
            // res.end('success');
            //将字符串转换位一个对象
            console.log("接受到页面的指令 :",alldata);
            //将当前的指令转换为json 对象
            var jsonObj = JSON.parse(alldata);
            //处理业务
            //1. 不管 上级页面发送什么指令 这里一律根据ic_num 找到 socket 通道 进行消息 下发
            console.log("当前下发指令设备icNum :",jsonObj.icNum);


             //1.1 去tcp 缓存users 中找到当前ic_nunm的 conn
            if (sessions.sessions[jsonObj.icNum] != null) {

                sessions.sessions[jsonObj.icNum].conn.write(jsonObj.data);
                var retjson = {"data":"OK","errorCode":1,"flag":true};
                res.end(JSON.stringify(retjson));
            }else{
                var retjson ={"data":"当前设备离线 "+jsonObj.icNum +"","errorCode":-1,"flag":false};
                res.end(JSON.stringify(retjson));
            }


        });
    }


    if (req.url === '/sendConfig' && req.method.toLowerCase() === 'post') {
        var alldata = '';
        req.on('data', function (chunk) {
            alldata += chunk;
        });

        req.on('end', function () {
            // res.end('success');
            //将字符串转换位一个对象
            console.log("接受到页面的指令 :",alldata);
            //将当前的指令转换为json 对象
            var jsonObj = JSON.parse(alldata);
            console.log("当前下发指令设备icNum :",jsonObj.ic_num);

            var getobj = {
                "tablename" : "timing",
                "fields" : "ic_num",
                "extra_where": "and ic_num = "+jsonObj.ic_num

            }
            s_db.db_get(getobj, function (status, ret) {
                if (status == 1){
                   if (ret != null){

                       console.log("执行update");
                       var upobj = {
                           "tablename" : "timing",
                           "data" :{
                               "start_time":jsonObj.start_time,
                               "end_time" : jsonObj.end_time
                           },
                           "extra_where": "and ic_num = "+jsonObj.ic_num
                       }
                       s_db.db_update(upobj, function (status, ret) {
                           if (status ==1){

                               var retjson ={"data":"配置成功","errorCode":1,"flag":true};
                               res.end(JSON.stringify(retjson));
                           }else{
                               var retjson ={"data":"配置失败,请重试 ","errorCode":-1,"flag":false};
                               res.end(JSON.stringify(retjson));
                           }
                       });

                   }else{

                       console.log("执行insert")
                       var insobj = {
                           "tablename" : "timing",
                           "data" :{
                               "ic_num":jsonObj.ic_num,
                               "start_time":jsonObj.start_time,
                               "end_time" : jsonObj.end_time
                           }
                       }
                       s_db.db_insert(insobj, function (status, ret) {
                          if (status ==1){

                              var retjson ={"data":"配置成功 "+jsonObj.icNum +"","errorCode":1,"flag":true};
                              res.end(JSON.stringify(retjson));
                          }else{
                              var retjson ={"data":"配置失败,请重试 "+jsonObj.icNum +"","errorCode":-1,"flag":false};
                              res.end(JSON.stringify(retjson));
                          }
                       });
                   }
                }
            })




        });
    }
});




module.exports = http_server;